package com.xyoto.community.recover;

import com.xyoto.community.cache.CacheInstruction;
import com.xyoto.community.constant.DeleteStatus;
import com.xyoto.community.entity.main.Dynamic;
import com.xyoto.community.mapper.main.DynamicMapper;
import com.xyoto.community.service.DynamicCacheService;
import com.xyoto.community.utils.RedisCommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * Author: 廖振辉
 * Date: 2021/12/4
 * Description: 更新缓存中的动态数量，用于数据恢复
 */
@SpringBootTest
class LatestDynamicRecover {

    @Autowired
    private DynamicCacheService dynamicCacheService;

    @Autowired
    private DynamicMapper dynamicMapper;

    @Autowired
    private RedisCommonUtils redisCommonUtils;

    /**
     *
     * @author 廖振辉
     * @return {@link}
     */
    @Test
    public void updateDynamicCount() {
        // 删除redis中的动态数量
        redisCommonUtils.del(CacheInstruction.DYNAMIC_COUNT);
        List<Dynamic> dynamicList = dynamicMapper.getAllDynamic(DeleteStatus.NO);
//        HashedMap<Integer, Integer> map = new HashedMap<>();
        for (Dynamic dynamic : dynamicList) {
            dynamicCacheService.updateDynamicCountInCache(dynamic);
//            Integer count = map.getOrDefault(0,0);
//            map.put(0,count+1);
//            Integer topicId = dynamic.getTopicId();
//            if (topicId != null && topicId != 0) {
//                Integer orDefault = map.getOrDefault(topicId, 0);
//                map.put(topicId,orDefault+1);
//            }
        }
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        map.forEach((k,v)->{
//            redisCommonUtils.hincr(CacheInstruction.DYNAMIC_COUNT,k.toString(),v);
//        });
    }

    /**
     * 将最新的500条加入到redsi
     * @author 廖振辉
     * @return {@link}
     */
    @Test
    public void updateTotalDynamic2Cache(){
        List<Dynamic> dynamicList = dynamicMapper.getAllDynamic(DeleteStatus.NO);
        for (Dynamic dynamic : dynamicList) {
            dynamicCacheService.saveDynamic2LatestDynamicIdSet(dynamic);
        }
    }


}